1//////////////////////////////////////////////////////////////////////
2// LibFile: hinges.scad
3// Functions and modules for creating hinges and snap-locking hinged parts.
4// Includes:
5// include <BOSL2/std.scad>
6// include <BOSL2/hinges.scad>
7// FileGroup: Parts
8// FileSummary: Hinges and snap-locking hinged parts.
9//////////////////////////////////////////////////////////////////////
10
11include <rounding.scad>
12include <screws.scad>
13
14// Section: Hinges
15
16// Module: knuckle_hinge()
17// Synopsis: Creates a knuckle-hinge shape.
18// SynTags: Geom
19// Topics: Hinges, Parts
20// See Also: living_hinge_mask(), snap_lock(), snap_socket()
21// Usage:
22// knuckle_hinge(length, offset, segs, [inner], [arm_height=], [arm_angle=], [fill=], [clear_top=], [gap=], [round_top=], [round_bot=], [knuckle_diam=], [pin_diam=], [pin_fn=], [anchor=], [spin=], [orient=]) [ATTACHMENTS];
23// Description:
24// Construct standard knuckle hinge in two parts using a hinge pin that must be separately supplied.
25// The default is configured to use a piece of 1.75 mm filament as the hinge pin, but you can select
26// any dimensions you like to use a screw or other available pin material. The BOTTOM of the hinge
27// is its mount point, which is aligned with the hinge pin centersurface, and the hinge pin hole is
28// the CENTER of the hinge. The offset is the distance from a vertical mounting point to the center
29// of the hinge pin. The hinge barrel is held by an angled support and vertical support. The
30// length of the angled support is determined by its angle and the offset. You specify the length
31// of the vertical support with the arm_height parameter.
32// .
33// A hinge requires clearance so its parts don't interfere. If the hinge pin is exactly centered on
34// the top of your part, then the hinge may not close all the way due to interference at the edge.
35// A small clearance, specified with `clearance=`, raises the hinge up and can ease this
36// interference. It should probably be equal to a layer thickness or two. If the hinge knuckle is
37// close to the hinged part then the mating part may interfere. You can create clearance to address
38// this problem by increasing the offset to move the hinge knuckles farther away. Another method is
39// to cut out a curved recess on the parts to allow space for the other hinges. This is possible
40// using the `knuckle_clearance=` parameter, which specifies the extra space to cut away to leave
41// room for the hinge knuckles. It must be positive for any space to be cut, and to use this option
42// you must make the hinge a child of some object and specify {{diff()}} for the parent object of
43// the hinge.
44// Figure(2D,Med,NoScales): The basic hinge form appears on the left. If fill is set to true the gap between the mount surface and hinge arm is filled as shown on the right.
45// _knuckle_hinge_profile(4, 5, $fn=32, fill=false);
46// right(13)_knuckle_hinge_profile(4, 5, $fn=32, fill=true);
47// fwd(9)stroke([[0,0],[4,4],[4,9]], width=.3,color="black");
48// stroke([[5,-5],[5,0]], endcaps="arrow2", color="blue",width=.15);
49// color("blue"){move([6.2,-2.5])text("arm_height",size=.75,valign="center");
50// stroke(arc(r=3, cp=[0,-9], angle=[47,90],$fn=64),width=.15,endcaps="arrow2");
51// move([-.5,-6])text("arm_angle", size=0.75,halign="right");
52// move([14,-4])text("fill=true", size=1);
53// }
54// Continues:
55// As shown in the above figure, the fill option fills the gap between the hinge arm and the mount surface to make a stronger connection. When the
56// arm height is set to zero, only a single segment connects the hinge barrel to the mount surface.
57// Figure(2D,Med,NoScales): Zero arm height with 45 deg arm
58// right(10) _knuckle_hinge_profile(4, 0, $fn=32);
59// _knuckle_hinge_profile(4, 0, $fn=32,fill=false);
60// right(11)fwd(-3)color("blue")text("fill=true",size=1);
61// right(.5)fwd(-3)color("blue")text("fill=false",size=1);
62// Continues:
63// Figure(2D,Med,NoScales): Zero arm height with 90 deg arm. The clear_top parameter removes the hinge support material that is above the x axis
64// _knuckle_hinge_profile(4, 0, 90, $fn=32);
65// right(10) _knuckle_hinge_profile(4, 0, 90, $fn=32,clear_top=true);
66// right(9.5)fwd(-3)color("blue")text("clear_top=true",size=.76);
67// right(.5)fwd(-3)color("blue")text("clear_top=false",size=.76);
68// Figure(2D,Med,NoScales): An excessively large clearance value raises up the hinge center. Note that the hinge mounting remains bounded by the X axis, so when `fill=true` or `clear_top=true` this is different than simply raising up the entire hinge.
69// right(10) _knuckle_hinge_profile(4, 0, 90, $fn=32,clear_top=true,clearance=.5);
70// _knuckle_hinge_profile(4, 0, $fn=32,fill=true,clearance=.5);
71// Continues:
72// For 3D printability, you may prefer a teardrop shaped hole, which you can get with `teardrop=true`;
73// if necessary you can specify the teardrop direction to be UP, DOWN, FORWARD, or BACK.
74// (These directions assume that the base of the hinge is mounted on the back of something.)
75// Another option for printability is to use an octagonal hole, though it does seem more
76// difficult to size these for robust printability. To get an octagonal hole set `pin_fn=8`.
77// Figure(2D,Med,NoScales): Alternate hole shapes for improved 3D printabililty
78// right(10) _knuckle_hinge_profile(4, 0, $fn=32,pin_fn=8);
79// _knuckle_hinge_profile(4, 0, $fn=32,tearspin=0);
80// right(11)fwd(-3)color("blue")text("octagonal",size=1);
81// right(1.5)fwd(-3)color("blue")text("teardrop",size=1);
82// Continues:
83// The default pin hole size admits a piece of 1.75 mm filament. If you prefer to use a machine
84// screw you can set the pin_diam to a screw specification like `"M3"` or "#6". In this case,
85// a clearance hole is created through most of the hinge with a self-tap hole for the last segment.
86// If the last segment is very long you may shrink the self-tap portion using the tap_depth parameter.
87// The pin hole diameter is enlarged by the `2*$slop` for numerically specified holes.
88// Screw holes are made using {{screw_hole()}} which enlarges the hole by `4*$slop`.
89// .
90// To blend hinges better with a model you can round off the joint with the mounting surface using
91// the `round_top` and `round_bot` parameters, which specify the cut distance, the amount of material to add.
92// They make a continuous curvature "smooth" roundover with `k=0.8`. See [smooth roundovers](rounding.scad#section-types-of-roundovers) for more
93// information. If you specify too large of a roundover you will get an error that the rounding doesn't fit.
94// Figure(2D,Med,NoScales): Top and bottom roundovers for smooth hinge attachment
95// right(12)_knuckle_hinge_profile(6, 0, $fn=32,fill=false,round_top=1.5);
96// _knuckle_hinge_profile(4, 0, $fn=32,fill=false,round_bot=1.5);
97// right(12)fwd(11)color("blue")text("round_top=1.8",size=1);
98// right(.5)fwd(-3)color("blue")text("round_bot=1.5",size=1);
99// Arguments:
100// length = total length of the entire hinge
101// offset = horizontal offset of the hinge pin center from the mount point
102// segs = number of hinge segments
103// inner = set to true for the "inner" hinge. Default: false
104// ---
105// arm_height = vertical height of the arm that holds the hinge barrel. Default: 0
106// arm_angle = angle of the arm down from the vertical. Default: 45
107// fill = if true fill in space between arm and mount surface. Default: true
108// clear_top = if true remove any excess arm geometry that appears above the top of the mount surface. Default: false
109// gap = gap between hinge segments. Default: 0.2
110// round_top = rounding amount to add where top of hinge arm joins the mount surface. Generally only useful when fill=false. Default: 0
111// round_bot = rounding amount to add where bottom of hinge arm joins the mount surface. Default: 0
112// knuckle_diam = diameter of hinge barrel. Default: 4
113// pin_diam = diameter of hinge pin hole as a number of screw specification. Default: 1.75
114// pin_fn = $fn value to use for the pin.
115// teardrop = Set to true or UP/DOWN/FWD/BACK to specify teardrop shape for the pin hole. Default: false
116// screw_head = screw head to use for countersink
117// screw_tolerance = screw hole tolerance. Default: "close"
118// tap_depth = Don't make the tapped part of the screw hole larger than this.
119// $slop = increases pin hole diameter
120// clearance = raises pin hole to create clearance at the edge of the mounted surface. Default: 0.15
121// clear_knuckle = clear space for hinge knuckle of mating part. Must use with {{diff()}}. Default: 0
122// anchor = Translate so anchor point is at origin (0,0,0). See [anchor](attachments.scad#subsection-anchor). Default: `BOTTOM`
123// spin = Rotate this many degrees around the Z axis after anchor. See [spin](attachments.scad#subsection-spin). Default: `0`
124// orient = Vector to rotate top towards, after spin. See [orient](attachments.scad#subsection-orient). Default: `UP`
125// Example: Basic hinge, inner=false in front and inner=true in the back
126// $fn=32;
127// ydistribute(30){
128// knuckle_hinge(length=35, segs=5, offset=3, arm_height=1);
129// knuckle_hinge(length=35, segs=5, offset=3, arm_height=1,inner=true);
130// }
131// Example(NoScales): Basic hinge, mounted. Odd segment count means the "outside" hinge is on the outside at both ends.
132// $fn=32;
133// cuboid([2,40,15])
134// position(TOP+RIGHT) orient(anchor=RIGHT)
135// knuckle_hinge(length=35, segs=9, offset=3, arm_height=1);
136// Example(NoScales): Corresponding inner hinge to go with previous example. Note that the total number of hinge segments adds to the 9 specified.
137// $fn=32;
138// cuboid([2,40,15])
139// position(TOP+RIGHT) orient(anchor=RIGHT)
140// knuckle_hinge(length=35, segs=9, offset=3, arm_height=1, inner=true);
141// Example(NoScales): This example shows how to position and orient the hinge onto the front of an object instead of the right side.
142// $fn=32;
143// cuboid([40,2,15])
144// position(TOP+FRONT) orient(anchor=FWD)
145// knuckle_hinge(length=35, segs=9, offset=3, arm_height=1);
146// Example(NoScales): Hinge with round_bot set to create a smooth transition, but octagonal hinge pin holes for printing
147// $fn=32;
148// cuboid([2,40,15])
149// position(TOP+RIGHT) orient(anchor=RIGHT)
150// knuckle_hinge(length=35, segs=9, offset=3, arm_height=1,
151// round_bot=1, pin_fn=8);
152// Example(NoScales): Hinge with no vertical arm, just angled arm
153// $fn=32;
154// cuboid([2,40,15])
155// position(TOP+RIGHT) orient(anchor=RIGHT)
156// knuckle_hinge(length=35, segs=9, offset=3, pin_fn=8);
157// Example(NoScales): Setting the arm_angle to a large value like 90 produces a hinge that doesn't look great
158// $fn=32;
159// cuboid([2,40,15])
160// position(TOP+RIGHT) orient(anchor=RIGHT)
161// knuckle_hinge(length=35, segs=9, offset=3, arm_angle=90,
162// arm_height=0, pin_fn=8);
163// Example(NoScales): The above hinge is improved with clear_top, which allows nice attachment to a shape half the thickness of the hinge barrel
164// $fn=32;
165// cuboid([20,40,2])
166// position(TOP+RIGHT) orient(anchor=RIGHT)
167// knuckle_hinge(length=35, segs=9, offset=3, arm_height=0,
168// arm_angle=90, pin_fn=8, clear_top=true);
169// Example(NoScales): Uneven hinge using seg_ratio. Here the inner hinge segments are 1/3 the outer, a rather extreme difference. Note also that it's a little simpler to mount the inner hinge on the LEFT side of the top section to interface with the hinge mounted on the RIGHT.
170// $fn=32;
171// cuboid([2,40,15]){
172// position(TOP+RIGHT) orient(anchor=RIGHT)
173// knuckle_hinge(length=35, segs=9, offset=3, arm_height=1,
174// seg_ratio=1/3);
175// attach(TOP,TOP) color("green")
176// cuboid([2,40,15],anchor=TOP)
177// position(TOP+LEFT) orient(anchor=LEFT)
178// knuckle_hinge(length=35, segs=9, offset=3, arm_height=1,
179// seg_ratio=1/3, inner=true);
180// }
181// Example(NoScales): A single hinge with an even number of segments will probably look strange, but they work together neatly in a pair. This example also shows that the arm_height can change between the inner and outer hinge parts and they will still interface properly.
182// $fn=32;
183// cuboid([2,40,15]){
184// yflip_copy()
185// position(TOP+RIGHT+FRONT) orient(anchor=RIGHT)
186// knuckle_hinge(length=12, segs=2, offset=2, arm_height=2,
187// anchor=BOT+LEFT);
188// attach(TOP,TOP) color("green")
189// cuboid([2,40,15],anchor=TOP)
190// yflip_copy()
191// position(TOP+LEFT+FRONT) orient(anchor=LEFT)
192// knuckle_hinge(length=12, segs=2, offset=2, arm_height=0,
193// inner=true, anchor=BOT+RIGHT);
194// }
195// Example(NoScales): Hinge with self-tapping screw hole. Note that last segment has smaller diameter for screw to bite, whereas other segments have clearance holes.
196// $fn=32;
197// bottom_half(z=.01)
198// cuboid([2,40,15],anchor=TOP)
199// position(TOP+RIGHT) orient(anchor=RIGHT)
200// knuckle_hinge(length=35, segs=5, offset=5, knuckle_diam=9, pin_diam="#6", fill=false,inner=false, screw_head="flat");
201// Example(NoScales): If you give a non-flat screw head then a counterbore for that head is generated. If you don't want the counterbore, don't give a head type. In this example, tap_depth limits the narrower self-tap section of the hole.
202// $fn=32;
203// bottom_half(z=.01)
204// cuboid([2,40,15],anchor=TOP)
205// position(TOP+RIGHT) orient(anchor=RIGHT)
206// knuckle_hinge(length=35, segs=3, offset=5, knuckle_diam=9, pin_diam="#6",
207// fill=false, inner=false, tap_depth=6, screw_head="socket");
208// Example(NoScales): This hinge has a small offset, so the hinged parts may interfere. To prevent this, use `knuckle_clearance`. This example shows an excessive clearance value to make the effect obvious. Note that you **must** use {{diff()}} when you set `knuckle_clearance`, and the hinge must be a child of the object it mounts to. Otherwise the cylinders that are supposed to be subtracted will appear as extra objects. This is an inner hinge, so it has clearance zones for the larger outer hinge that will mate with it.
209// $fn=32;
210// diff()
211// cuboid([4,40,15])
212// position(TOP+RIGHT) orient(anchor=RIGHT)
213// knuckle_hinge(length=35, segs=5, offset=2, inner=true, knuckle_clearance=1);
214// Example(NoScales): Oh no! Forgot to use {{diff()}} with knuckle_clearance!
215// $fn=32;
216// cuboid([4,40,15])
217// position(TOP+RIGHT) orient(anchor=RIGHT)
218// knuckle_hinge(length=35, segs=5, offset=2, inner=true, knuckle_clearance=1);
219
220function knuckle_hinge(length, segs, offset, inner=false, arm_height=0, arm_angle=45, gap=0.2,
221 seg_ratio=1, knuckle_diam=4, pin_diam=1.75, fill=true, clear_top=false,
222 round_bot=0, round_top=0, pin_fn, clearance,
223 tap_depth, screw_head, screw_tolerance="close",
224 anchor=BOT,orient,spin) = no_function("hinge");
225
226module knuckle_hinge(length, segs, offset, inner=false, arm_height=0, arm_angle=45, gap=0.2,
227 seg_ratio=1, knuckle_diam=4, pin_diam=1.75, fill=true, clear_top=false,
228 round_bot=0, round_top=0, pin_fn, clearance=0, teardrop,
229 tap_depth, screw_head, screw_tolerance="close", knuckle_clearance,
230 anchor=BOT,orient,spin)
231{
232 dummy =
233 assert(is_str(pin_diam) || all_positive([pin_diam]), "pin_diam must be a screw spec string or a positive number")
234 assert(all_positive(length), "length must be a postive number")
235 assert(is_int(segs) && segs>=2, "segs must be an integer 2 or greater")
236 assert(is_finite(offset) && offset>=knuckle_diam/2, "offset must be a valid number that is not smaller than radius of the hinge knuckle")
237 assert(is_finite(arm_angle) && arm_angle>0 && arm_angle<=90, "arm_angle must be greater than zero and less than or equal to 90");
238 segs1 = ceil(segs/2);
239 segs2 = floor(segs/2);
240 seglen1 = gap + (length-(segs-1)*gap) / (segs1 + segs2*seg_ratio);
241 seglen2 = gap + (length-(segs-1)*gap) / (segs1 + segs2*seg_ratio) * seg_ratio;
242 z_adjust = segs%2==1 ? 0
243 : inner? seglen1/2
244 : seglen2/2;
245 tearspin = is_undef(teardrop) || teardrop==false ? undef
246 : teardrop==UP || teardrop==true ? 0
247 : teardrop==DOWN ? 180
248 : teardrop==BACK ? 270
249 : teardrop==FWD ? 90
250 : assert(false, "Illegal value for teardrop");
251 knuckle_segs = segs(knuckle_diam);
252 transform = down(offset)*yrot(-90)*zmove(z_adjust);
253
254 if(knuckle_clearance){
255 knuckle_clearance_diam = knuckle_diam / cos(180/knuckle_segs) + 2*knuckle_clearance;
256 tag("remove")
257 attachable(anchor,spin,orient,
258 size=[length,
259 arm_height+offset/tan(arm_angle)+knuckle_diam/2+knuckle_diam/2/sin(arm_angle),
260 offset+knuckle_diam/2],
261 offset=[0,
262 -arm_height/2-offset/tan(arm_angle)/2-knuckle_diam/sin(arm_angle)/4+knuckle_diam/4,
263 -offset/2+knuckle_diam/4]
264 )
265 {
266 multmatrix(transform) down(segs%2==1? 0 : (seglen1+seglen2)/2){
267 move([offset,clearance])
268 intersection(){
269 n = inner && segs%2==1 ? segs1
270
271 : inner ? segs1
272 : segs2;
273 zcopies(n=n, spacing=seglen1+seglen2)
274 cyl(h=(inner?seglen1:seglen2)+gap-.01, d=knuckle_clearance_diam, circum=true, $fn=knuckle_segs, realign=true);
275 //cyl(h=length+2*gap, d=knuckle_clearance_diam, circum=true, $fn=knuckle_segs, realign=true);
276 }
277 }
278 union(){}
279 }
280 }
281 attachable(anchor,spin,orient,
282 size=[length,
283 arm_height+offset/tan(arm_angle)+knuckle_diam/2+knuckle_diam/2/sin(arm_angle),
284 offset+knuckle_diam/2],
285 offset=[0,
286 -arm_height/2-offset/tan(arm_angle)/2-knuckle_diam/sin(arm_angle)/4+knuckle_diam/4,
287 -offset/2+knuckle_diam/4]
288 )
289 {
290 multmatrix(transform)
291 force_tag() difference() {
292 zcopies(n=inner?segs2:segs1, spacing=seglen1+seglen2)
293 linear_extrude((inner?seglen2:seglen1)-gap,center=true)
294 _knuckle_hinge_profile(offset=offset, arm_height=arm_height, arm_angle=arm_angle, knuckle_diam=knuckle_diam, pin_diam=pin_diam,
295 fill=fill, clear_top=clear_top, round_bot=round_bot, round_top=round_top, pin_fn=pin_fn,clearance=clearance,tearspin=tearspin);
296 if (is_str(pin_diam)) back(clearance)right(offset) up(length/2-(inner?1:1)*z_adjust) zrot(default(tearspin,0)){
297 $fn = default(pin_fn,$fn);
298 tap_depth = min(segs%2==1?seglen1-gap/2:seglen2-gap/2, default(tap_depth, length));
299 screw_hole(pin_diam, length=length+.01, tolerance="self tap", bevel=false, anchor=TOP, teardrop=is_def(tearspin));
300 multmatrix(inner ? zflip(z=-length/2) : IDENT)
301 if (is_undef(screw_head) || screw_head=="none" || starts_with(screw_head,"flat"))
302 screw_hole(pin_diam, length=length-tap_depth, tolerance=screw_tolerance, bevel=false, anchor=TOP, head=screw_head, teardrop=is_def(tearspin));
303 else {
304 screw_hole(pin_diam, length=length-tap_depth, tolerance=screw_tolerance, bevel=false, anchor=TOP, teardrop=is_def(tearspin));
305 screw_hole(pin_diam, length=.01, tolerance=screw_tolerance, bevel=false, anchor=TOP, head=screw_head, teardrop=is_def(tearspin));
306 }
307 }
308 }
309 children();
310 }
311}
312
313
314module _knuckle_hinge_profile(offset, arm_height, arm_angle=45, knuckle_diam=4, pin_diam=1.75, fill=true, clear_top=false, round_bot=0, round_top=0, pin_fn, clearance=0, tearspin)
315{
316 extra = .01;
317 skel = turtle(["left", 90-arm_angle, "untilx", offset+extra, "left", arm_angle,
318 if (arm_height>0) each ["move", arm_height]]);
319 ofs = arm_height+offset/tan(arm_angle);
320 start=round_bot==0 && round_top==0 ? os_flat(abs_angle=90)
321 : os_round(abs_angle=90, cut=[-round_top,-round_bot],k=.8);
322 back(clearance)
323 difference(){
324 union(){
325 difference(){
326 fwd(ofs){
327 left(extra)offset_stroke(skel, width=knuckle_diam, start=start);
328 if (fill) polygon([each list_head(skel,-2), fwd(clearance,last(skel)), [-extra,ofs-clearance]]);
329 }
330 if (clear_top==true || clear_top=="all") left(.1)fwd(clearance) rect([offset+knuckle_diam,knuckle_diam+1+clearance],anchor=BOT+LEFT);
331 if (is_num(clear_top)) left(.1)fwd(clearance) rect([.1+clear_top, knuckle_diam+1+clearance], anchor=BOT+LEFT);
332 }
333 right(offset)ellipse(d=knuckle_diam,realign=true,circum=true);
334 }
335 if (is_num(pin_diam) && pin_diam>0){
336 $fn = default(pin_fn,$fn);
337 right(offset)
338 if (is_def(tearspin)){
339 teardrop2d(d=pin_diam+2*get_slop(), realign=true, circum=true, spin=tearspin);
340 }
341 else ellipse(d=pin_diam+2*get_slop(), realign=true, circum=true);
342 }
343 }
344}
345
346
347// Module: living_hinge_mask()
348// Synopsis: Creates a mask to make a folding "living" hinge.
349// SynTags: Geom
350// Topics: Hinges, Parts
351// See Also: knuckle_hinge(), living_hinge_mask(), snap_lock(), snap_socket(), apply_folding_hinges_and_snaps()
352// Usage:
353// living_hinge_mask(l, thick, [layerheight=], [foldangle=], [hingegap=], [$slop=], [anchor=], [spin=], [orient=]) [ATTACHMENTS];
354// Description:
355// Creates a mask to be differenced away from a plate to create a "live" hinge, where a thin layer of plastic holds two parts together.
356// Center the mask at the bottom of the part you want to make a hinge in.
357// The mask will leave hinge material `2*layerheight` thick on the bottom of the hinge.
358// Arguments:
359// l = Length of the hinge in mm.
360// thick = Thickness in mm of the material to make the hinge in.
361// ---
362// layerheight = The expected printing layer height in mm.
363// foldangle = The interior angle in degrees of the joint to be created with the hinge. Default: 90
364// hingegap = Size in mm of the gap at the bottom of the hinge, to make room for folding.
365// $slop = Increase size of hinge gap by double this amount
366// anchor = Translate so anchor point is at origin (0,0,0). See [anchor](attachments.scad#subsection-anchor). Default: `CENTER`
367// spin = Rotate this many degrees around the Z axis. See [spin](attachments.scad#subsection-spin). Default: `0`
368// orient = Vector to rotate top towards. See [orient](attachments.scad#subsection-orient). Default: `UP`
369// Example:
370// living_hinge_mask(l=100, thick=3, foldangle=60);
371module living_hinge_mask(l, thick, layerheight=0.2, foldangle=90, hingegap=undef, anchor=CENTER, spin=0, orient=UP)
372{
373 hingegap = default(hingegap, layerheight)+2*get_slop();
374 size = [l, hingegap, 2*thick];
375 size2 = [l, hingegap+2*thick*tan(foldangle/2)];
376 attachable(anchor,spin,orient, size=size, size2=size2) {
377 up(layerheight*2) prismoid([l,hingegap], [l, hingegap+2*thick/tan(foldangle/2)], h=thick, anchor=BOT);
378 children();
379 }
380}
381
382module folding_hinge_mask(l, thick, layerheight=0.2, foldangle=90, hingegap=undef, anchor=CENTER, spin=0, orient=UP)
383{
384 deprecate("living_hinge_mask");
385 living_hinge_mask(l, thick, layerheight, foldangle, hingegap, anchor, spin, orient);
386}
387
388
389
390// Section: Snap Locks
391
392
393// Module: apply_folding_hinges_and_snaps()
394// Synopsis: Adds snap shapes and removes living hinges from a child shape.
395// SynTags: Geom
396// Topics: Hinges, Parts
397// See Also: knuckle_hinge(), living_hinge_mask(), snap_lock(), snap_socket()
398// Usage:
399// apply_folding_hinges_and_snaps(thick, [foldangle=], [hinges=], [snaps=], [sockets=], [snaplen=], [snapdiam=], [hingegap=], [layerheight=], [$slop=]) CHILDREN;
400// Description:
401// Adds snaplocks and create hinges in children at the given positions.
402// Arguments:
403// thick = Thickness in mm of the material to make the hinge in.
404// foldangle = The interior angle in degrees of the joint to be created with the hinge. Default: 90
405// hinges = List of [LENGTH, POSITION, SPIN] for each hinge to difference from the children.
406// snaps = List of [POSITION, SPIN] for each central snaplock to add to the children.
407// sockets = List of [POSITION, SPIN] for each outer snaplock sockets to add to the children.
408// snaplen = Length of locking snaps.
409// snapdiam = Diameter/width of locking snaps.
410// hingegap = Size in mm of the gap at the bottom of the hinge, to make room for folding.
411// layerheight = The expected printing layer height in mm.
412// ---
413// $slop = increase hinge gap by twice this amount
414// Example(Med):
415// size=100;
416// apply_folding_hinges_and_snaps(
417// thick=3, foldangle=acos(1/3),
418// hinges=[
419// for (a=[0,120,240], b=[-size/2,size/4]) each [
420// [200, polar_to_xy(b,a), a+90]
421// ]
422// ],
423// snaps=[
424// for (a=[0,120,240]) each [
425// [rot(a,p=[ size/4, 0 ]), a+90],
426// [rot(a,p=[-size/2,-size/2.33]), a-90]
427// ]
428// ],
429// sockets=[
430// for (a=[0,120,240]) each [
431// [rot(a,p=[ size/4, 0 ]), a+90],
432// [rot(a,p=[-size/2, size/2.33]), a+90]
433// ]
434// ]
435// ) {
436// $fn=3;
437// difference() {
438// cylinder(r=size-1, h=3);
439// down(0.01) cylinder(r=size/4.5, h=3.1, spin=180);
440// down(0.01) for (a=[0:120:359.9]) zrot(a) right(size/2) cylinder(r=size/4.5, h=3.1);
441// }
442// }
443module apply_folding_hinges_and_snaps(thick, foldangle=90, hinges=[], snaps=[], sockets=[], snaplen=5, snapdiam=5, hingegap=undef, layerheight=0.2)
444{
445 hingegap = default(hingegap, layerheight)+2*get_slop();
446 difference() {
447 children();
448 for (hinge = hinges) {
449 translate(hinge[1]) {
450 living_hinge_mask(
451 l=hinge[0], thick=thick, layerheight=layerheight,
452 foldangle=foldangle, hingegap=hingegap, spin=hinge[2]
453 );
454 }
455 }
456 }
457 for (snap = snaps) {
458 translate(snap[0]) {
459 snap_lock(
460 thick=thick, snaplen=snaplen, snapdiam=snapdiam,
461 layerheight=layerheight, foldangle=foldangle,
462 hingegap=hingegap, spin=snap[1]
463 );
464 }
465 }
466 for (socket = sockets) {
467 translate(socket[0]) {
468 snap_socket(
469 thick=thick, snaplen=snaplen, snapdiam=snapdiam,
470 layerheight=layerheight, foldangle=foldangle,
471 hingegap=hingegap, spin=socket[1]
472 );
473 }
474 }
475}
476
477
478
479// Module: snap_lock()
480// Synopsis: Creates a snap-lock shape.
481// SynTags: Geom
482// Topics: Hinges, Parts
483// See Also: knuckle_hinge(), living_hinge_mask(), snap_lock(), snap_socket()
484// Usage:
485// snap_lock(thick, [snaplen=], [snapdiam=], [layerheight=], [foldangle=], [hingegap=], [$slop=], [anchor=], [spin=], [orient=]) [ATTACHMENTS];
486// Description:
487// Creates the central snaplock part.
488// Arguments:
489// thick = Thickness in mm of the material to make the hinge in.
490// ---
491// snaplen = Length of locking snaps.
492// snapdiam = Diameter/width of locking snaps.
493// layerheight = The expected printing layer height in mm.
494// foldangle = The interior angle in degrees of the joint to be created with the hinge. Default: 90
495// hingegap = Size in mm of the gap at the bottom of the hinge, to make room for folding.
496// $slop = increase size of hinge gap by double this amount
497// anchor = Translate so anchor point is at origin (0,0,0). See [anchor](attachments.scad#subsection-anchor). Default: `CENTER`
498// spin = Rotate this many degrees around the Z axis. See [spin](attachments.scad#subsection-spin). Default: `0`
499// orient = Vector to rotate top towards. See [orient](attachments.scad#subsection-orient). Default: `UP`
500// Example:
501// snap_lock(thick=3, foldangle=60);
502module snap_lock(thick, snaplen=5, snapdiam=5, layerheight=0.2, foldangle=90, hingegap=undef, anchor=CENTER, spin=0, orient=UP)
503{
504 hingegap = default(hingegap, layerheight)+2*get_slop();
505 snap_x = (snapdiam/2) / tan(foldangle/2) + (thick-2*layerheight)/tan(foldangle/2) + hingegap/2;
506 size = [snaplen, snapdiam, 2*thick];
507 attachable(anchor,spin,orient, size=size) {
508 back(snap_x) {
509 cube([snaplen, snapdiam, snapdiam/2+thick], anchor=BOT) {
510 attach(TOP) xcyl(l=snaplen, d=snapdiam, $fn = max(16,quant(segs(snapdiam/2),4)));
511 attach(TOP) xcopies(snaplen-snapdiam/4/3) xscale(0.333) sphere(d=snapdiam*0.8, $fn = max(12,quant(segs(snapdiam/2),4)));
512 }
513 }
514 children();
515 }
516}
517
518
519// Module: snap_socket()
520// Synopsis: Creates a snap-lock socket shape.
521// SynTags: Geom
522// Topics: Hinges, Parts
523// See Also: knuckle_hinge(), living_hinge_mask(), snap_lock(), snap_socket()
524// Usage:
525// snap_socket(thick, [snaplen=], [snapdiam=], [layerheight=], [foldangle=], [hingegap=], [$slop=], [anchor=], [spin=], [orient=]) [ATTACHMENTS];
526// Description:
527// Creates the outside snaplock socketed part.
528// Arguments:
529// thick = Thickness in mm of the material to make the hinge in.
530// ---
531// snaplen = Length of locking snaps.
532// snapdiam = Diameter/width of locking snaps.
533// layerheight = The expected printing layer height in mm.
534// foldangle = The interior angle in degrees of the joint to be created with the hinge. Default: 90
535// hingegap = Size in mm of the gap at the bottom of the hinge, to make room for folding.
536// $slop = Increase size of hinge gap by double this amount
537// anchor = Translate so anchor point is at origin (0,0,0). See [anchor](attachments.scad#subsection-anchor). Default: `CENTER`
538// spin = Rotate this many degrees around the Z axis. See [spin](attachments.scad#subsection-spin). Default: `0`
539// orient = Vector to rotate top towards. See [orient](attachments.scad#subsection-orient). Default: `UP`
540// Example:
541// snap_socket(thick=3, foldangle=60);
542module snap_socket(thick, snaplen=5, snapdiam=5, layerheight=0.2, foldangle=90, hingegap=undef, anchor=CENTER, spin=0, orient=UP)
543{
544 hingegap = default(hingegap, layerheight)+2*get_slop();
545 snap_x = (snapdiam/2) / tan(foldangle/2) + (thick-2*layerheight)/tan(foldangle/2) + hingegap/2;
546 size = [snaplen, snapdiam, 2*thick];
547 attachable(anchor,spin,orient, size=size) {
548 fwd(snap_x) {
549 zrot_copies([0,180], r=snaplen+get_slop()) {
550 diff("divot")
551 cube([snaplen, snapdiam, snapdiam/2+thick], anchor=BOT) {
552 attach(TOP) xcyl(l=snaplen, d=snapdiam, $fn=max(16,quant(segs(snapdiam/2),4)));
553 tag("divot") attach(TOP) left((snaplen+snapdiam/4/3)/2) xscale(0.333) sphere(d=snapdiam*0.8, $fn = max(12,quant(segs(snapdiam/2),4)));
554 }
555 }
556 }
557 children();
558 }
559}
560
561
562
563// vim: expandtab tabstop=4 shiftwidth=4 softtabstop=4 nowrap